package cn.bdqn.service.impl;

import cn.bdqn.common.error.LoginError;
import cn.bdqn.entity.Staff;
import cn.bdqn.dao.StaffDao;
import cn.bdqn.entity.vo.UserVo;
import cn.bdqn.service.StaffService;
import cn.bdqn.util.JwtUtil;
import cn.hutool.core.date.DateField;
import cn.hutool.core.date.DateTime;
import cn.hutool.core.date.DateUtil;
import net.sf.jsqlparser.expression.DateTimeLiteralExpression;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import javax.annotation.Resource;

@Service
public class StaffServiceImpl implements StaffService {
    @Resource
    private StaffDao staffDao;

    @Override
    public List<Staff> queryAll() {
        return this.staffDao.queryAll();
    }
    
    @Override
    public Staff queryById(Integer staffId) {
        return this.staffDao.queryById(staffId);
    }
    @Override
    public Staff insert(Staff staff) {
        this.staffDao.insert(staff);
        return staff;
    }

    @Override
    public Staff update(Staff staff) {
        this.staffDao.update(staff);
        return this.queryById(staff.getStaffId());
    }

    @Override
    public boolean deleteById(Integer staffId) {
        return this.staffDao.deleteById(staffId) > 0;
    }

    @Override
    public UserVo login(String uname, String pwd) throws LoginError {
        Staff staff = staffDao.login(uname,pwd);
        UserVo userVo = new UserVo();
        if(null==staff){
            throw new LoginError("用户名或密码错误");
        }else {
            userVo.setUsername(staff.getUsername());
            userVo.setToken(JwtUtil.createToken(staff));
        }

        return userVo;
    }
}
